#include "FiveOS2.ch"

#define PU_ARBITRARY         4
#define GPIF_DEFAULT         0
#define GPIT_MICRO        4096   //   0x1000
#define GPIA_ASSOC       16384   //   0x4000

#define DEVESC_ERROR        -1
#define DEVESC_STARTDOC  33104   //   0x8150
#define DEVESC_ENDDOC    33105   //   0x8151
#define DEVESC_NEWFRAME  90880   //  0x16300

//----------------------------------------------------------------------------//

CLASS TPrinter

   DATA   hDC, hPS

   DATA   cDriver, cQueue, cDevice
   DATA   cDocument

   METHOD New( cDocument )   CONSTRUCTOR

   METHOD End()

   METHOD NewFrame() INLINE DevEscape( ::hDC, DEVESC_NEWFRAME )
   METHOD NewPage() INLINE DevEscape( ::hDC, DEVESC_NEWFRAME )

   METHOD Move( nY, nX ) INLINE GpiMove( ::hPS, nY, nX )

   METHOD Box( nWidth, nHeight, nMode, nVRound, nHRound ) ;
      INLINE GpiBox( ::hPS, nMode, nWidth, nHeight, nHRound, nVRound )

   METHOD GetLastError() INLINE WinGetLastError()

ENDCLASS

//----------------------------------------------------------------------------//

METHOD New( cDocument ) CLASS TPrinter

   local cPrinter := PrfQueryString( 0, "PM_SPOOLER", "PRINTER" )
   local cInfo
   local aPageSize := { 0, 0 }

   DEFAULT cDocument := "FiveOS2"

   if Empty( cPrinter )
      MsgAlert( "Not printer available!" )
      return nil
   endif

   cInfo = PrfQueryString( 0, "PM_SPOOLER_PRINTER",;
                           SubStr( cPrinter, 1, Len( cPrinter ) - 1 ) )

   ::cDriver   = StrToken( cInfo, 2, ";" )

   if At( ".", ::cDriver ) != 0
      ::cDriver = StrToken( ::cDriver, 2, "." )
   endif

   ::cQueue    = StrToken( cInfo, 3, ";" )
   ::cDevice   = ::cDriver

   MsgInfo( "cDriver:" + ::cDriver )
   MsgInfo( "cQueue:" + ::cQueue )
   MsgInfo( "cDevice:" + ::cDevice )

   ::hDC       = DevOpenDC( ::cDriver, ::cQueue, ::cDevice )
   ::cDocument = cDocument

   if ::hDC != 0
      ::hPS = GpiCreatePS( GetHab(), ::hDC, aPageSize,;
                 nOr( PU_ARBITRARY, GPIF_DEFAULT, GPIT_MICRO, GPIA_ASSOC ) )
   endif

   if DevEscape( ::hDC, DEVESC_STARTDOC, Len( ::cDocument ), ::cDocument ) ;
                 == DEVESC_ERROR
      MsgAlert( "Printer initialization error" )
   endif

return nil

//----------------------------------------------------------------------------//

METHOD End() CLASS TPrinter

   DevEscape( ::hDC, DEVESC_ENDDOC )
   GpiDestroyPS( ::hPS )
   ::hPS = 0
   DevCloseDC( ::hDC )
   ::hDC = 0

return nil

//----------------------------------------------------------------------------//
